/**
 * @requires SuperMap/Util.js
 * @requires SuperMap/Layer/CanvasLayer.js
 */

/**
 * Class: SuperMap.Layer.Tianditu 此图层可以访问 天地图 的地图服务。
 *
 * Inherits from: - <SuperMap.Layer.CanvasLayer>
 */
SuperMap.Layer.TDT = SuperMap
    .Class(
    SuperMap.CanvasLayer,
    {

        /**
         * APIProperty: name {String}图层名称，默认为“Tianditu”，防止初始化时未设置图层名
         *
         */
        name: "Tianditu",

        /**
         * Property: url {String}默认的AMap的地图服务器地址
         */
        /*
         * Tianditu底图(球面墨卡托投影)
         */
        url: "http://t0.tianditu.com/DataServer?T=vec_w&X=${x}&Y=${y}&L=${z}",

        /**
         * Constructor: SuperMap.Layer.AMap
         * 创建天地图图层，可以浏览天地图地图 Example: (code)
         *
         * var layer = new SuperMap.Layer.Tianditu(layerName, type, projection);
         * type {vec, img, label}
         * //将Layer图层加载到Map对象上 map.addLayer(layer);
         * //出图，map.setCenter函数显示地图 //加载的天地图图层默认为墨卡托投影，所以定位需要转换
         * map.setCenter( new SuperMap.LonLat(110,39.5 ).transform(
         * new SuperMap.Projection("EPSG:4326"),
         * map.getProjectionObject() ), 4 );
         *
         * (end)
         *
         *
         * Parameters: name - {String} 图层名称
         *               layerType - {String} 图层类型，包括底图、底图标签、影像、影像标签
         */
        initialize: function (name, layerType, projection) {
            this.name = name;
            var baseUrl = "http://t3.tianditu.com/DataServer?T=";
            if (layerType == "vec")
                this.url = baseUrl + "vec_c";//地图
            else if (layerType == "img")
                this.url = baseUrl + "img_c";//影像
            else if (layerType == "vecLabel")
                this.url = baseUrl + "cva_";
            else if (layerType == "img_label")
                this.url = baseUrl + "cia_c";//标签
            else if (layerType == "ter")
                this.url = baseUrl + "ter_";
            else if (layerType == "terLabel")
                this.url = baseUrl + "cta_";

            var options = {
                projection: "EPSG:4326",
                numZoomLevels: 15
            };
            if (projection == 1) {   //WGS84 经纬度
                this.url += "c";
                options.projection = "EPSG:4490"
            } else if (projection == 2) { //EPSG:900913 球面墨卡托
                this.url += "w";
                options.projection = "EPSG:900913";
            }
            this.url += "&X=${x}&Y=${y}&l=${z}&tk=28b495e4df789d971d2ae77b01a55a55";

            this.url = this.proxyUrl(this.url);
            SuperMap.CanvasLayer.prototype.initialize.apply(this, [
                name, this.url, {}, options ]);
        },

        proxyUrl: function (url) {
            var purl = this.getBaseUrl() + "/app/proxy/tile?url=";
            var _param = url;  //this.url;
            _param = this.replaceChar(_param);
            return purl + _param;
        },

        getBaseUrl: function () {
            var loc = window.location;
            if (loc.origin) {
                return loc.origin;
            }
            return loc.protocol + "//" + loc.host;
        },

        replaceChar: function (url) {
            url = url.replace(/&/g, "$");
            url = url.replace(/\?/g, "!");
            return url;
        },

        /**
         * Method: clone
         */
        clone: function (obj) {
            if (obj == null) {
                obj = new SuperMap.Layer.TDT(this.name);
            }
            obj = SuperMap.CanvasLayer.prototype.clone.apply(this,
                [ obj ]);
            return obj;
        },

        /**
         * APIMethod: destroy 解构Tianditu类，释放资源。
         */
        destroy: function () {
            var me = this;
            me.name = null;
            me.url = null;
            SuperMap.CanvasLayer.prototype.destroy.apply(me,
                arguments);
        },
        /**
         * Method: getTileUrl 获取瓦片的URL。
         *
         * Parameters: xyz - {Object} 一组键值对，表示瓦片X, Y, Z方向上的索引。
         *
         * Returns {String} 瓦片的 URL 。
         */
        getTileUrl: function (xyz) {
            var me = this, url;
            url = me.url;
            url = SuperMap.String.format(url, {
                x: xyz.x,
                y: xyz.y,
                z: xyz.z
            });
            return url;
        },
        CLASS_NAME: "SuperMap.Layer.TDT"
    });
